package extend.org.compiere.hrm;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.TimeZone;

import org.adempiere.exceptions.AdempiereException;
import org.compiere.model.Mhrmsalaryemployee;
import org.compiere.model.Mhrmsheduleemployee;
import org.compiere.model.Mhrmtimesheet;
import org.compiere.model.Mhrmworktime;

public class AverageCalc {
	
	public static AverageSalary getAvarage (int C_BPartner_ID, Timestamp dt){
		/** */
		AverageSalary list = new AverageSalary();
		/**  */
		IndexSalary[] sumAmount = null;	
		/** */
		Mhrmsalaryemployee salary = null;
		/** */
		Calendar Month = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
		Timestamp sdt = null;
		Timestamp edt = null;
		
		BigDecimal SUM = new BigDecimal(0);
		BigDecimal BSUM = new BigDecimal(0);
								
		int hquota = 0;
		int shedule = 0;	
		int DAYS = 0;
		
		double HOURS = 0;
		double result = 0.;		
		
		/** Get previous date and calculate one year */
		if(new org.joda.time.DateTime(dt.getTime()).getDayOfMonth() <= 1)
			Month.setTime(new org.joda.time.DateTime(dt.getTime()).plusDays(5).toDate());
		else 
			Month.setTimeInMillis(dt.getTime());		
		dt = new Timestamp(Month.getTimeInMillis());
		
		Month.setTime(dt);
		Month.set(Calendar.DATE, Month.getActualMaximum(Calendar.MONTH));
		Month.add(Calendar.MONTH, -1);
		Month.set(Calendar.DATE, Month.getActualMaximum(Calendar.DATE));
		Month.add(Calendar.DAY_OF_MONTH, -1);
		edt = new Timestamp(Month.getTimeInMillis());
		
		Month.setTime(dt);
		Month.set(Calendar.DATE, Month.getMinimum(Calendar.MONTH));		
		Month.add(Calendar.MONTH, -11);
		Month.add(Calendar.DAY_OF_MONTH, -1);
		Month.set(Calendar.DATE,0);
		sdt = new Timestamp(Month.getTimeInMillis());	
		
		sumAmount = IndexSalary.getSumAmout(C_BPartner_ID, sdt, edt);
		BSUM = IndexSalary.getSumBonus(C_BPartner_ID, edt);
		
		//
		SUM = sumAmount[0].getTotalind();
		DAYS = sumAmount[0].getWorkdays();
		
		// if never take amount before
		if (SUM.doubleValue() == 0.){
			// Take amount from previous month
			salary = Mhrmsalaryemployee.getSalary(C_BPartner_ID, dt);
			shedule = Mhrmsheduleemployee.getSheduleRule(C_BPartner_ID, dt);
			if (shedule == 0)
				throw new AdempiereException (" Shedule setting not set " + C_BPartner_ID);
			if (salary == null){
				throw new AdempiereException (" Salary or timesheet for person is empty " + C_BPartner_ID);
			}
			HOURS = Mhrmworktime.getWorkHours(shedule, dt);			
			SUM = salary.gethrm_amountsalary().add(salary.gethrm_salaryincrement());
			SUM = SUM.divide(new BigDecimal(HOURS),2);			
			hquota = Mhrmtimesheet.getHourquota(C_BPartner_ID, dt);
			
			if (hquota == 0)
				throw new AdempiereException (" Get run timesheet procedure for this person: " + C_BPartner_ID);				
			SUM = SUM.multiply(new BigDecimal(hquota));
			DAYS = hquota;			
		}	
		
		SUM = SUM.add(BSUM);
		if(DAYS != 0)
			result = SUM.doubleValue()/DAYS;
		else
			result = 0.;
		
		list.setHOURQUOTA(DAYS);
		list.setRESULT(result);
		list.setSTARTDATE(sdt);
		list.setENDDATE(edt);
						
		return list;
		
	}

}


